package com.app.jpa.security.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.app.entity.User;
import com.app.jpa.security.SecurityDataService;
import com.app.to.Usuario;

@Service("jpaSecurityDataService")
@Repository
@Transactional
public class SecurityDataServiceImpl implements SecurityDataService {
	private Log log = LogFactory.getLog(SecurityDataServiceImpl.class);
	
	@PersistenceContext 
	private EntityManager em;

	@Transactional(readOnly = true)
	@Override
	public User validateLoginAccess(Usuario user) {
		try{
			TypedQuery<User> query = em.createNamedQuery("User.validateLoginAccess",User.class);
			query.setParameter("nombre", user.getUser());
			query.setParameter("clave", user.getPassword());
			User userEnt = query.getSingleResult();
			return userEnt;
			
		}catch(Exception e){
			
			return null;
		}
		
	}
	
	

}
